package com.fivestyle.service.impl;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import com.fivestyle.Ossutils.ConstPropertiesUtils;
import com.fivestyle.service.OssService;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

//import com.yang.config.ConstPropertiesUtils;
//import ConstPropertiesUtils;

@Service
@CrossOrigin
public class OssServiceImpl implements OssService {

    @Override
    public String UploadAvatarfile(MultipartFile file) throws IOException {
//        工具类获取固定值

//        @Autowired ConstPropertiesUtils ConstPropertiesUtils;

        String endpoint = ConstPropertiesUtils.EndPoint;
// 阿里云账号AccessKey拥有所有API的访k权限i风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = ConstPropertiesUtils.KeyId;
        String accessKeySecret= ConstPropertiesUtils.KeySecret;
        String buckName=ConstPropertiesUtils.BucketName;
        String fileName=file.getOriginalFilename();
        //获取上传文件输入流
        InputStream inputStream=file.getInputStream();
//        设置文件名称值
//        ewe-122-wwe
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        fileName=uuid+fileName;
//按日期分类
        String date = new DateTime().toString("yyyy/MM/dd");
        fileName=date+'/'+fileName;

// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
//        InputStream inputStream = new FileInputStream("D:\\localpath\\examplefile.txt");

        ossClient.putObject(buckName,fileName, inputStream);
        // 关闭OSSClient。
        ossClient.shutdown();
//        自定义返回路径
//        https://yang-halo.oss-cn-beijing.aliyuncs.com/image.jpg
            String url=null;
            url="https://"+buckName+'.'+endpoint+'/'+fileName;
        return url;
    }

    @Override
    public String bannerImage(MultipartFile file) throws IOException {

        String endpoint = ConstPropertiesUtils.EndPoint;
        String accessKeyId = ConstPropertiesUtils.KeyId;
        String accessKeySecret= ConstPropertiesUtils.KeySecret;
        String buckName=ConstPropertiesUtils.BucketName;
        String fileName=file.getOriginalFilename();
        //获取上传文件输入流
        InputStream inputStream=file.getInputStream();
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        fileName="FiveStyle/BannerImage"+'/'+uuid+fileName;

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(buckName,fileName, inputStream);
        ossClient.shutdown();
        String url=null;
        url="https://"+buckName+'.'+endpoint+'/'+fileName;
        return url;
    }

    @Override
    public String bookImage(MultipartFile file) throws IOException {
        String endpoint = ConstPropertiesUtils.EndPoint;
        String accessKeyId = ConstPropertiesUtils.KeyId;
        String accessKeySecret= ConstPropertiesUtils.KeySecret;
        String buckName=ConstPropertiesUtils.BucketName;
        String fileName=file.getOriginalFilename();
        //获取上传文件输入流
        InputStream inputStream=file.getInputStream();
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        fileName="FiveStyle/BookImage"+'/'+uuid+fileName;

        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(buckName,fileName, inputStream);
        ossClient.shutdown();
        String url=null;
        url="https://"+buckName+'.'+endpoint+'/'+fileName;
        return url;

    }
}
